package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.ShareGirlsOrderBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.DisplayUtil;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;
import com.voicechat.firstmet.view.ShadowDrawable;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: ShareGirlsOrderAdapter
 * @Description: 订单adapter
 * @Author: ljp
 * @CreateDate: 2019/1/23 16:34
 * @Version: 1.0
 */
public class ShareGirlsOrderAdapter extends BaseAdapter<ShareGirlsOrderBean.PageBean.ListBean> {
    private String flag;
    private String TAG = "ShareGirlsOrderAdapter";
    private OrderOnClick orderOnClick;

    public void setOrderOnClick(OrderOnClick orderOnClick) {
        this.orderOnClick = orderOnClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public ShareGirlsOrderAdapter(Context context, List<ShareGirlsOrderBean.PageBean.ListBean> tlist, int mLayout, String flag) {
        super(context, tlist, mLayout);
        this.flag = flag;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            ShareGirlsOrderBean.PageBean.ListBean listBean = tlist.get(position);
            ShadowDrawable.setShadowDrawable(itemViewHolder.view,
                    Color.parseColor("#ffffff"),
                    DisplayUtil.dp2px(context, 14),
                    Color.parseColor("#24666666"), DisplayUtil.dp2px(context, 15), 0, 0);
            if (!TextUtils.isEmpty(listBean.getAvatarUrl())) {
                if (!listBean.getAvatarUrl().equals(itemViewHolder.imageIcon.getTag(R.id.image_icon))) {
                    // 加载图片
                    itemViewHolder.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getAvatarUrl(),
                            itemViewHolder.imageIcon);
                    //setImage(listBean.getAvatarUrl(), viewHolder1.imageIcon);
                }
            }
            Log.i(TAG, "onBindViewHolder: ----listBean------"+listBean.toString());
            itemViewHolder.tvName.setText(listBean.getNickName());
            if (listBean.getvType() == 1) {//判断是vip等级 1 月 2 年 0 不是会员
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
            } else if (listBean.getvType() == 2) {
                itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
            } else {
                itemViewHolder.imageVip.setVisibility(View.GONE);
            }
            if (listBean.getGender() == 1) {//判断性别  1男 2女
                itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            } else if (listBean.getGender() == 2) {
                itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            if (!TextUtils.isEmpty(listBean.getBirthday())) {
                String substring = listBean.getBirthday().substring(0, 4);
                String time = Utils.getStringTime(new Date());
                int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                itemViewHolder.tvAge.setText(age + "");
            }
            if(listBean.getRmb() == 0){
                itemViewHolder.tvDataMoney.setText(listBean.getPrice()+"元");
            }else {
                itemViewHolder.tvDataMoney.setText(listBean.getRmb()+"元");
            }
            itemViewHolder.tvDataAddress.setText(listBean.getAddress());
            itemViewHolder.tvOrderTime.setText(listBean.getCreateDate());
           /* private int time;//时间 3(3天内) 5(5天内) 7(7天内)
            private int accompany;//陪同标记 1陪同 2不陪同
            private int receiptStatus;//接单状态 1拒绝 2同意 0默认
            private int complete;//完成状态 完成状态 1完成 2未完成 3取消 4已退款 5拒绝退款6申请退款7申请退款中
            private int paySign;//支付标记’支付标记 1未付款 2已付款 3退款申请 4已退款*/
            if (listBean.getTime() == 3) {
                itemViewHolder.tvDataTime.setText("3天内");
            } else if (listBean.getTime() == 5) {
                itemViewHolder.tvDataTime.setText("5天内");
            } else if (listBean.getTime() == 7) {
                itemViewHolder.tvDataTime.setText("7天内");
            }
            if (listBean.getAccompany() == 1) {
                itemViewHolder.tvDataWay.setText("闺蜜陪同");
            } else {
                itemViewHolder.tvDataWay.setText("无闺蜜陪同");
            }
            itemViewHolder.tvDataRemarks.setText(TextUtils.isEmpty(listBean.getRemarks())?"无":listBean.getRemarks());
            itemViewHolder.imageIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   orderOnClick.setPerson(listBean);
                }
            });
            if (flag.equals("receive")) {//接收订单者
                if (listBean.getComplete() == 1) {//判断订单是否完成，//完成状态 完成状态 1完成 2未完成 3取消 4已退款 5拒绝退款
                    itemViewHolder.imageState.setVisibility(View.VISIBLE);
                    itemViewHolder.imageState.setImageResource(R.mipmap.share_order_success);
                    itemViewHolder.linearState.setVisibility(View.GONE);
                } else if (listBean.getComplete() == 2) {//2未完成
                    //判断支付状态  paySign;//支付标记’支付标记 1未付款 2已付款 3退款申请 4已退款*/
                    switch (listBean.getPaySign()) {
                        case 1:// 1未付款
                            itemViewHolder.tvSureState.setVisibility(View.GONE);
                            itemViewHolder.tvCancel.setText("待支付");
                            break;
                        case 2:// 2已付款，//判断接单状态
                            if (listBean.getReceiptStatus() == 0) {//接单状态 1拒绝 2同意 0默认
                                itemViewHolder.tvSureState.setVisibility(View.VISIBLE);
                                itemViewHolder.tvSureState.setText("接受");
                                itemViewHolder.tvCancel.setText("拒绝");
                                itemViewHolder.tvCancel.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        orderOnClick.setReceiveOrderOnClick(listBean,1,itemViewHolder.tvCancel,itemViewHolder.tvSureState);//拒绝
                                    }
                                });
                            } else if (listBean.getReceiptStatus() == 2) {
                                itemViewHolder.tvSureState.setVisibility(View.GONE);
                                itemViewHolder.tvCancel.setText("取消订单");
                                itemViewHolder.tvCancel.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        orderOnClick.setReceiveOrderOnClick(listBean,3,itemViewHolder.tvCancel,itemViewHolder.tvSureState);//取消订单
                                    }
                                });
                            }
                            break;
                        case 4:// 4已退款
                            break;
                    }
                } else {//3取消 4已退款 5拒绝退款
                    itemViewHolder.imageState.setVisibility(View.VISIBLE);
                    itemViewHolder.imageState.setImageResource(R.mipmap.share_order_cacal);
                    itemViewHolder.linearState.setVisibility(View.GONE);
                }
                itemViewHolder.tvSureState.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderOnClick.setReceiveOrderOnClick(listBean,2,itemViewHolder.tvCancel,itemViewHolder.tvSureState);
                    }
                });
            } else {//下单者
                if (listBean.getComplete() == 1) {//判断订单是否完成，//判断订单是否完成，//完成状态 完成状态 1完成 2未完成 3取消 4已退款 5拒绝退款
                    itemViewHolder.imageState.setVisibility(View.VISIBLE);
                    itemViewHolder.imageState.setImageResource(R.mipmap.share_order_success);
                    itemViewHolder.linearState.setVisibility(View.GONE);
                } else if (listBean.getComplete() == 2) {//如果未完成判断是否取消
                    //判断支付状态  paySign;//支付标记’支付标记 1未付款 2已付款 3退款申请 4已退款*/
                    switch (listBean.getPaySign()) {
                        case 1:// 1未付款
                            itemViewHolder.tvSureState.setText("去支付");
                            itemViewHolder.tvCancel.setText("取消订单");
                            itemViewHolder.tvSureState.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    orderOnClick.setPlaceOrderOnClick(listBean,4,null,itemViewHolder.tvSureState);
                                }
                            });
                            break;
                        case 2:// 2已付款
                            itemViewHolder.tvSureState.setText("确认见面");
                            itemViewHolder.tvCancel.setText("取消订单");
                            itemViewHolder.tvSureState.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    orderOnClick.setPlaceOrderOnClick(listBean,2,null,null);
                                }
                            });
                            break;
                        case 4:// 4已退款
                            break;
                    }
                    itemViewHolder.tvCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            orderOnClick.setPlaceOrderOnClick(listBean,1,itemViewHolder.tvCancel,itemViewHolder.tvSureState);
                        }
                    });
                } else if(listBean.getComplete() == 7){//3取消 4已退款 5拒绝退款 6申请退款7申请退款中
                  /*  if(listBean.getPaySign() == 1){
                        itemViewHolder.imageState.setVisibility(View.VISIBLE);
                        itemViewHolder.imageState.setImageResource(R.mipmap.share_order_cacal);
                        itemViewHolder.tvSureState.setVisibility(View.GONE);
                        itemViewHolder.tvCancel.setVisibility(View.GONE);
                    }else {*/
                        itemViewHolder.imageState.setVisibility(View.VISIBLE);
                        itemViewHolder.imageState.setImageResource(R.mipmap.share_order_cacal);
                        itemViewHolder.tvSureState.setVisibility(View.GONE);
                        itemViewHolder.tvCancel.setText("申请退款");
                        itemViewHolder.tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                orderOnClick.setPlaceOrderOnClick(listBean,3,null,null);
                            }
                        });
                   // }
                }else {
                    itemViewHolder.imageState.setVisibility(View.VISIBLE);
                    itemViewHolder.imageState.setImageResource(R.mipmap.share_order_cacal);
                    itemViewHolder.tvSureState.setVisibility(View.GONE);
                    itemViewHolder.tvCancel.setVisibility(View.GONE);
                }
            }
        }
    }


    class ItemViewHolder extends BaseViewHolder {
        @BindView(R.id.image_state)
        ImageView imageState;
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.tv_nike_name)
        TextView tvName;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.linear_state)
        LinearLayout linearState;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_cancel)
        TextView tvCancel;
        @BindView(R.id.tv_sure_state)
        TextView tvSureState;
        @BindView(R.id.tv_data_address)
        TextView tvDataAddress;
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.tv_data_remarks)
        TextView tvDataRemarks;
        @BindView(R.id.tv_data_time)
        TextView tvDataTime;
        @BindView(R.id.tv_data_way)
        TextView tvDataWay;
        @BindView(R.id.tv_data_money)
        TextView tvDataMoney;
        View view;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            view = itemView;
        }
    }

    public interface OrderOnClick{
        //下单者 //1取消订单   2完成订单   3申请退款 4去支付
        void setPlaceOrderOnClick(ShareGirlsOrderBean.PageBean.ListBean listBean,int flag,TextView textViewCancel,TextView tvSure);
        //接收订单者  //2接收  1拒绝   3取消订单
        void setReceiveOrderOnClick(ShareGirlsOrderBean.PageBean.ListBean listBean,int flag,TextView textViewCancel,TextView tvSure);

        void setPerson(ShareGirlsOrderBean.PageBean.ListBean listBean);
    }


}
